﻿using NCP.Model;
using NCP.Core;
using NCP.Service;
using NCP.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;

namespace WebApp.Controllers
{
    public class RoleController : BaseController<Role>
    {
        private RoleService _service;
        private MemberService _memberService;
        public RoleController(RoleService service, MemberService memberService)
        {
            _service = service;
            _memberService = memberService;
            _baseService = service;
        }

        // GET: Role
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult RoleAdd(int Id)
        {
            Role item = new Role();
            if (Id>0)
                item = _service.Get(Id);
            return View(item);
        }

        //用户授权 
        public ActionResult UserInRole()
        {
            return View();
        }

        //角色下人员信息
        public ActionResult RelMemberList(string roleId)
        {
            return View();
        }

        public ActionResult GetListByRole()
        {
            Expressionable<Member> exp = new Expressionable<Member>();

            exp.AndIF(!string.IsNullOrEmpty(Request["RoleId"]), t => SqlFunc.Subqueryable<MemberInRole>().Where(s => s.Code == t.Code && s.RoleId == Request["RoleId"].ObjToInt()).Any());
            List<Member> dataList = _memberService.GetAll(exp);
            //dataList.ForEach(d => {
            //    CommonUtil.BuildText<PartyMember>(d);
            //});
            dataList.ForEach(t => {
                CommonUtil.BuildText<Member>(t);
            });
            LayData ht = new LayData("0", "获取成功", dataList, dataList.Count);

            return Content(JsonHelp.SerializeObject(ht));
        }

        //删除人员tag信息
        public ActionResult RemoveUserRole(string userCode, string RoleId)
        {
            bool rst = _service.RemoveUserRole(userCode, RoleId);
            LayData ht = new LayData("0", rst ? "去除角色成功" : "去除角色失败");
            return Content(JsonConvert.SerializeObject(ht));
        }

        //授权
        public ActionResult AddUserRole(string code)
        {
            List<Role> roleList = _service.GetRolesById(code);
            Member member = _memberService.GetByStaffCode(code);
            ViewBag.UserName = member.Name;
            ViewBag.UserCode = member.Code;
            return View(roleList);
        }

        public ActionResult GetPmsByRole(string roleId)
        {
            Hashtable ht = new Hashtable();
            List<Permission> pmsList = _service.GetPmsByRoleId(roleId);
            ht.Add("rows", pmsList);
            return Json(ht);
        }
        public ActionResult Grant()
        {
            string roleId = Request.Form["RoleId"];
            string rights = Request.Form["Rights"];
            List<PmsInRole> pmsInRoles = new List<PmsInRole>();
            var rs = rights.Split('|');
            foreach (var r in rs)
            {
                PmsInRole item = new PmsInRole();
                item.PmsId = r.ObjToInt();
                item.RoleId = roleId.ObjToInt();
                pmsInRoles.Add(item);
            }
            _service.GrantRights(pmsInRoles);

            LayData ajaxRst = new LayData("0", "授权成功");
            return Json(ajaxRst);

        }

        public ActionResult GrantRole()
        {
            string staffCode = Request["UserCode"];
            LayData rst;
            if (!string.IsNullOrEmpty(Request["Role"]))
            {
                JToken token = JToken.Parse(Request["Role"]);
                List<MemberInRole> userInRoles = new List<MemberInRole>();

                foreach (var r in token.Values())
                {
                    MemberInRole item = new MemberInRole();
                    item.RoleId = r.Value<int>();
                    if (item.RoleId == 0)
                        continue;
                    item.Code = staffCode;
                    userInRoles.Add(item);
                }

                _service.GrantRoles(userInRoles);

                rst = new LayData("0", "授权成功");
            }
            else
            {
                rst = new LayData("0", "授权失败");
            }
            return Json(rst);
        }

        public ActionResult GetList()
        {
            Expressionable<Role> exp = new Expressionable<Role>();
            int pageIndex = 1;
            int pageSize = 20;
            if (!string.IsNullOrEmpty(Request.Form["page"]))
            {
                pageIndex = Convert.ToInt32(Request.Form["page"]);
            }
            if (!string.IsNullOrEmpty(Request.Form["limit"]))
            {
                pageSize = Convert.ToInt32(Request.Form["limit"]);
            }
            PageModel pageModel = new PageModel() { PageIndex = pageIndex, PageSize = pageSize };
            List<Role> dataList = _service.GetPageList(exp, pageModel);
            LayData ht = new LayData("0", "获取成功", dataList, pageModel.TotalCount);

            return Content(JsonConvert.SerializeObject(ht));
        }
    }
}